package com.ktjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.mapper.ChanceMapper;
import com.ktjy.pojo.Chance;
import com.ktjy.pojo.Customer;
import com.ktjy.service.IChanceService;
import com.ktjy.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjx
 * @since 2025-01-15
 */
@Service
public class ChanceServiceImpl extends ServiceImpl<ChanceMapper, Chance> implements IChanceService {
    @Autowired
    ICustomerService iCustomerService;
    @Override
    public boolean successChance(Long id) {
        //1.修改销售机会状态
        //根据销售机会编号，查询销售机会信息
        Chance chance=this.getById(id);
        chance.setChcStatus("3");
        boolean flag1=this.updateById(chance);

        //2.新增客户
        Customer customer=new Customer();
        //客户编号
        int rand= (int)(Math.random()*(1000-100)+100);
        SimpleDateFormat df=new SimpleDateFormat("yyMMddHHmmss");
        customer.setCustNo("KH"+df.format(new Date())+rand);

        customer.setCustName(chance.getChcCustName());
        customer.setCustRegion(chance.getChcCustRegion());
        customer.setCustManagerId(chance.getChcDueId());
        customer.setCustManagerName(chance.getChcDueTo());
        customer.setCustLevel(Integer.valueOf(chance.getChcCustLevel()));
        customer.setCustSatisfy(5);
        customer.setCustCredit(5);
        customer.setCustTel(chance.getChcTel());
        customer.setCustStatus("1");
        //新增客户
        boolean flag2=iCustomerService.save(customer);

        return flag1 && flag2;
    }

    @Override
    public boolean failChance(Long id) {
        //根据销售机会编号，查询销售机会信息
        Chance chance=this.getById(id);
        //修改销售机会的状态
        chance.setChcStatus("4");
        //修改销售机会状态
        return this.updateById(chance);
    }
}
